#include <iostream>
#include <map>

using namespace std;

struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
		val(x), next(NULL) {
	}
};

ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2) {
	ListNode *tmp = pHead1;
	map<ListNode*, int> s;
	while (tmp != NULL)
	{
		s[tmp]++;
		tmp = tmp->next;
	}
	tmp = pHead2;
	while (tmp != NULL)
	{
		if (s[tmp] > 0)
			return tmp;
		tmp = tmp->next;
	}
	return NULL;

}

